/*
 * @Description: 首页验证 Store
 * @Author: xuyang
 * @Date: 2019-06-09 15:06:14 
 * @Last Modified by: XuYang
 * @Last Modified time: 2019-06-15 22:06:49
 */

import { observable, action, computed, runInAction } from 'mobx';
import {
  getCoinApi,
  getNssAccountApi,
  takeMoneyApi,
  getCoinRecordApi,
  chargeCoinRecordApi,
  getPreOutApi,
  getNoticeApi
} from '../api/home';

export default class Login {
    @observable coin = {
        coinname:'nss',
        address: '',
        instructions:'',
        min_instructions:'',
        addressImg: ''
    }; 
    @observable  account = 0;

    @observable charge = 0;

    @observable getPage = 1;
    @observable getTotal = 0;
    @observable getPageSize = 5;
    @observable getLastPage = 1;
    @observable getList = [];
    
    @observable recordPage = 1;
    @observable recordTotal = 0;
    @observable recordPageSize = 5;
    @observable recordLastPage = 1;
    @observable recordList = [];

    @observable notice = [];//通告
    constructor(rootStore) {
        this.rootStore = rootStore;
    }
    @action setCoin (coin){
        this.coin = {
            coinname: coin.coinname,
            address: coin.address.address,
            instructions: coin.instructions,
            min_instructions: coin.min_instructions,
            addressImg: coin.address_image_base64.replace('\\','')
        };
    }
    @action getCoin (){
        return this.coin;
    }
    @action setAccount (account){
        this.account = account;
    }
    @action getAccount (){
        return this.account;
    }
    @action setGetPage(page,pageSize, total,lastPage){
        this.getPage = page;
        this.getPageSize = pageSize;
        this.getTotal = total;
        this.getLastPage = lastPage;
    }   
    @action setRecordPage(page,pageSize, total,lastPage){
        this.recordPage = page;
        this.recordPageSize = pageSize;
        this.recordTotal = total;
        this.recordLastPage = lastPage;
    }   
    @action setGetList(list) {
        this.getList = list;
    }
    @action addGetList(list){
        this.getList = [...this.getList , ...list];
    }
    @action setRecordList(list) {
        this.recordList = list;
    }
    @action addRecordList(list){
        this.recordList = [...this.recordList , ...list];
    }
    @action setCharge(charge) {
        this.charge = isNaN(charge) ? 1 : parseInt(charge);
    }
    @action getCharge (){
        return this.charge;
    }
    @action setNotice (notice) {
        this.notice = notice;
    }
    /**
     * 获取充币地址
     */
    @action getCoinData = async () => {
        try { 
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await getCoinApi(ret);
                    if(response.status === 11001){
                        //登录失效 重新登录
                        this.rootStore.loginStore.loginInvalid();
                        return;
                    }
                    if (response.status === 10000) {
                        runInAction(() => {
                            this.setCoin(response.data);
                        });
                    } else if(response.status === 90001){
                        //数据为空
                    } else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
    @action getNssAccount = async ()=>{
        try {
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await getNssAccountApi(ret);
                    if(response.status === 11001){
                        //登录失效 重新登录
                        this.rootStore.loginStore.loginInvalid();
                        return;
                    }
                    if (response.status === 10000) {
                        runInAction(() => {
                            this.setAccount(response.data.balance);
                        });
                    } else if(response.status === 90001){
                        //数据为空
                    } else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
    @action takeMoney = async (params, cb) => {
        try {
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await takeMoneyApi(ret, params);
                    if(response.status === 11001){
                        //登录失效 重新登录
                        this.rootStore.loginStore.loginInvalid();
                        return;
                    }
                    if (response.status === 10000) {
                        runInAction(() => {
                            cb && cb();
                        });
                    } else if(response.status === 90001){
                        //数据为空
                    } else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
    /**
     * 提币记录
     */
    @action getCoinRecord = async (page) => {
        try {
            page = page || {page: 1};
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await getCoinRecordApi(ret,page);
                    if(response.status === 11001){
                        //登录失效 重新登录
                        this.rootStore.loginStore.loginInvalid();
                        return;
                    }
                    if(response.status !== 10000 && page.page > 1){
                        return;
                    }
                    if (response.status === 10000) {
                        runInAction(() => {
                            if(page.page !== this.getPage && page.page !== 1){
                                this.addGetList(response.data.data)
                            }else {
                                this.setGetList(response.data.data);
                            }
                            this.setGetPage(response.data.current_page,response.data.per_page,response.data.total,response.data.last_page);
                        });
                    } else if(response.status === 90001){
                        //数据为空
                    } else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
    /**
     * 充币记录
     */
    @action chargeCoinRecord = async (page) => {
        try {
            page = page || {page: 1};
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await chargeCoinRecordApi(ret,page);
                    if(response.status === 11001){
                        //登录失效 重新登录
                        this.rootStore.loginStore.loginInvalid();
                        return;
                    }
                    if(response.status !== 10000 && page.page > 1){
                        return;
                    } 
                    if (response.status === 10000) {
                        runInAction(() => {
                            if(page.page !== this.recordPage && page.page !== 1){
                                this.addRecordList(response.data.data)
                            }else {
                                this.setRecordList(response.data.data);
                            }
                            this.setRecordPage(response.data.current_page,response.data.per_page,response.data.total,response.data.last_page);
                        });
                    } else if(response.status === 90001){
                        //数据为空
                    } else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                    this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
    @action getPreOut = async () =>{
        try {
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await getPreOutApi(ret);
                    if(response.status === 11001){
                        //登录失效 重新登录
                        this.rootStore.loginStore.loginInvalid();
                        return;
                    }
                    if (response.status === 10000) {
                        // console.warn(response);
                        runInAction(() => {
                            this.setCharge(response.data.fee);
                        });
                    } else if(response.status === 90001){
                        //数据为空
                    } else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                    this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
    /**
     * 首页 通告
     */
    @action getNotice = async (cb) => {
        try {
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await getNoticeApi(ret);
                    if(response.status === 11001){
                        //登录失效 重新登录
                        this.rootStore.loginStore.loginInvalid();
                        return;
                    }  
                    if (response.status === 10000) {
                        runInAction(() => {
                            this.setNotice(response.data);
                            cb && cb()
                        });
                    } else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                    this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
}